package com.alex;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.anythink.core.api.ATAdConst;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationSplashRequestInfo;

import org.json.JSONObject;

import java.util.Map;

public class AlexGromoreConfig {
    //server
    public String mUnitType;
    public String mBannerSize;//for banner "320*50","300*250","720*90"；
    ATAdConst.CURRENCY mCurrency = ATAdConst.CURRENCY.RMB;

    //local extra
    int adSound = -1; //0: mute, 1:no mute
    public String mRewardName;
    public int mRewardAmount;
    public String mUserId;
    public String mUserData;
    public int mWidth;
    public int mHeight;
    public int mAdCount;//for native
    public boolean canInterrupt = false;//for native draw
    public Bitmap videoPlayBitmap = null;//for native draw
    public int videoPlaySize = 0;//for native draw
    public MediationSplashRequestInfo splashRequestInfo;//for splash

    private Context mContext;

    public AlexGromoreConfig(Context context, Map<String, Object> serverMap) {
        mContext = context.getApplicationContext();
        mUnitType = "1";
        mBannerSize = "320x50";
        mAdCount = 1;


        //local
        mRewardName = "";
        mRewardAmount = -1;
        mUserId = "";
        mUserData = "";
        mWidth = context.getResources().getDisplayMetrics().widthPixels;
        mHeight = context.getResources().getDisplayMetrics().heightPixels;
        parseServerData(serverMap);
    }

    void parseServerData(Map<String, Object> serverMap) {

        if (!serverMap.containsKey("slot_info")) {
            return;
        }

        String settingJsonString = serverMap.get("slot_info").toString();

        if (settingJsonString != null) {
            try {
                JSONObject setting = new JSONObject(settingJsonString);
                JSONObject common = setting.optJSONObject("common");

                parseCommon(common);

            } catch (Throwable e) {
                e.printStackTrace();
            }
        }

        try {
            Object checkCurrency = serverMap.get("gm_currency");
            if (checkCurrency != null && checkCurrency instanceof ATAdConst.CURRENCY) {
                mCurrency = (ATAdConst.CURRENCY) checkCurrency;
            }
        } catch (Throwable t) {

        }
    }

    private void parseCommon(JSONObject jsonObject) {
        if (jsonObject != null) {
            String unit_type = jsonObject.optString("unit_type");
            String size = jsonObject.optString("size");

            mUnitType = unit_type;
            mBannerSize = size;//banner size string
        }
    }


    void parseLocalData(Map<String, Object> localMap) {
        parseWidthHeight(localMap);

        try {
            if (localMap.containsKey(ATAdConst.KEY.AD_SOUND)) {
                adSound = Integer.parseInt(localMap.get(ATAdConst.KEY.AD_SOUND).toString());
            }
        } catch (Throwable e) {

        }

        String user_id = localMap.get(ATAdConst.KEY.USER_ID) != null ? localMap.get(
                ATAdConst.KEY.USER_ID).toString() : "";
        String user_custom_data = localMap.get(
                ATAdConst.KEY.USER_CUSTOM_DATA) != null ? localMap.get(
                ATAdConst.KEY.USER_CUSTOM_DATA).toString() : "";

        if (!TextUtils.isEmpty(user_id)) {
            mUserId = user_id;
        }
        if (!TextUtils.isEmpty(user_custom_data)) {
            mUserData = user_custom_data;
        }

    }

    void parseInterstitialLocalData(Map<String, Object> localMap) {
        parseLocalData(localMap);
    }

    void parseRewardLocalData(Map<String, Object> localMap) {
        parseLocalData(localMap);
        String reward_name = localMap.get(ATAdConst.KEY.REWARD_NAME) != null ? localMap.get(
                ATAdConst.KEY.REWARD_NAME).toString() : "";
        String reward_amount = localMap.get(ATAdConst.KEY.REWARD_AMOUNT) != null ? localMap.get(
                ATAdConst.KEY.REWARD_AMOUNT).toString() : "";

        if (!TextUtils.isEmpty(reward_name)) {
            mRewardName = reward_name;
        }
        try {
            if (!TextUtils.isEmpty(reward_amount)) {
                mRewardAmount = Integer.parseInt(reward_amount);
            }
        } catch (Throwable e) {
        }

    }

    void parseBannerLocalData(Map<String, Object> localMap) {
        //update from server extra
        if (!TextUtils.isEmpty(mBannerSize)) {
            switch (mBannerSize) {
                case "320x100":
                    mWidth = dip2px(mContext, 320);
                    mHeight = dip2px(mContext, 100);
                    break;
                case "300x250":
                    mWidth = dip2px(mContext, 300);
                    mHeight = dip2px(mContext, 250);
                    break;
                case "468x60":
                    mWidth = dip2px(mContext, 468);
                    mHeight = dip2px(mContext, 60);
                    break;
                case "728x90":
                    mWidth = dip2px(mContext, 728);
                    mHeight = dip2px(mContext, 90);
                    break;
                case "320x50":
                case "custom":
                default:
                    mWidth = dip2px(mContext, 320);
                    mHeight = dip2px(mContext, 50);
                    break;
            }
        }
        //update local extra
        parseWidthHeight(localMap);
    }

    void parseSplashDefaultData(Map<String, Object> localMap) {
        parseLocalData(localMap);
        try {
            if (localMap.containsKey(AlexGromoreConst.SPLASH_AD_REUQEST_INFO)) {
                Object requestInfoObj = localMap.get(AlexGromoreConst.SPLASH_AD_REUQEST_INFO);
                if (requestInfoObj instanceof MediationSplashRequestInfo) {
                    splashRequestInfo = (MediationSplashRequestInfo) requestInfoObj;
                }
            }
        } catch (Throwable t) {
        }
    }

    void parseNativeDefaultData(Map<String, Object> localMap) {
        parseLocalData(localMap);
        Object canInterruptObject = localMap.get(AlexGromoreConst.NATIVE_AD_INTERRUPT_VIDEOPLAY);
        Object videoPlayBitmapObject = localMap.get(
                AlexGromoreConst.NATIVE_AD_VIDEOPLAY_BTN_BITMAP);
        Object videoPlaySizeObject = localMap.get(AlexGromoreConst.NATIVE_AD_VIDEOPLAY_BTN_SIZE);

        if (canInterruptObject instanceof Boolean) {
            canInterrupt = Boolean.parseBoolean(canInterruptObject.toString());
        }

        if (videoPlayBitmapObject instanceof Bitmap) {
            videoPlayBitmap = (Bitmap) videoPlayBitmapObject;
        }

        if (videoPlaySizeObject instanceof Integer) {
            videoPlaySize = Integer.parseInt(videoPlaySizeObject.toString());
        }

    }

    private void parseWidthHeight(Map<String, Object> localMap) {
        String width = localMap.containsKey(ATAdConst.KEY.AD_WIDTH) ? localMap.get(
                ATAdConst.KEY.AD_WIDTH).toString() : "";
        Object heightObject = null;

        if (localMap.containsKey("tt_image_height")) {
            heightObject = localMap.get("tt_image_height");
        } else if (localMap.containsKey(ATAdConst.KEY.AD_HEIGHT)) {
            heightObject = localMap.get(ATAdConst.KEY.AD_HEIGHT);
        }

        int originWidth = mWidth;
        int originHeight = mHeight;
        try {
            if (!TextUtils.isEmpty(width)) {
                mWidth = (int) Double.parseDouble(width);
            }
        } catch (Throwable e) {
        }
        try {
            if (heightObject != null) {
                mHeight = (int) Double.parseDouble(heightObject.toString());
            }
        } catch (Throwable e) {
        }

        if (mWidth <= 0) {
            mWidth = originWidth;
        }

        if (mHeight < 0) {
            mHeight = originHeight;
        }
    }

    private static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / (scale <= 0 ? 1 : scale) + 0.5f);
    }

    private static int dip2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

}
